package com.yhao.simpledemo.controller;

import com.yhao.simpledemo.model.LoginInfo;
import com.yhao.simpledemo.model.Person;
import com.yhao.simpledemo.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : yhao
 * @Date : 2023/5/31 - 11:27
 * @Desc : TODO
 */
@RestController
@Slf4j
public class FileBeatController {

    @GetMapping("/online")
    public String online(Integer seconds) {
        System.out.println("seconds============>>>" + seconds);
        return "success";
    }

    @PostMapping("/organ/login")
    public String login(@RequestBody LoginInfo loginInfo) {
        log.info("loginInfo=========>>>>>" + loginInfo);
        return JsonUtil.objectToJson(loginInfo);
    }


    List list = new ArrayList();

    @GetMapping("/getAll")
    public String getAll() {

        Person person = null;
        if (list == null || list.size() == 0) {

            for (int i = 0; i < 10; i++) {
                person = new Person(i, "xm" + i, 20 + i);
                list.add(person);
            }
        }
        return JsonUtil.listToJson(list);
    }

    @PostMapping("/add")
    public String add(Person person) {
        list.add(person);
        return "success";
    }

    @GetMapping("/removeById/{id}")
    public String removeById(@PathVariable Integer id) {
        Person person = null;
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                person = (Person) list.get(i);
                if (id == person.getId()) {
                    list.remove(person);
                    return "success";
                }
            }
        }
        return "fail";
    }

}
